package com.captain.special.链表;

import com.captain.leetcode.链表.ListNode;

import java.util.List;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/3/23 12:40
 */
public class 删除排序链表中的重复元素82 {
    public static void main(String[] args) {
        new 删除排序链表中的重复元素82().deleteDuplicates(ListNode.getInstance());
    }

    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) return head;
        // 1 2 3 3 4 4 5
        ListNode dummy = new ListNode(-1, head);
        ListNode cur = dummy;

        while (head.next != null) {
            int val = head.val;
            int nextValue = head.next.val;
            if (val != nextValue) {
                head = head.next;
                cur = cur.next;
                continue;
            }
            //如果相等
            //找到下一个不相等的
            int val1 = head.val;
            while (head.next != null) {
                int val2 = head.next.val;
                if (val1 == val2) {
                    head = head.next;
                    continue;
                }
                head = head.next;
                break;
            }
            //临界条件 ,最后一个head的值
            if (head.val == val1) {
                cur.next = null;
            } else {
                cur.next = head;
            }
        }
        return dummy.next;
    }
}
